package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MesMaterial;
import com.ruoyi.system.service.IMesMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 原料Controller
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@RestController
@RequestMapping("/system/material")
public class MesMaterialController extends BaseController
{
    @Autowired
    private IMesMaterialService mesMaterialService;

    /**
     * 查询原料列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesMaterial mesMaterial)
    {
        startPage();
        List<MesMaterial> list = mesMaterialService.selectMesMaterialList(mesMaterial);
        return getDataTable(list);
    }

    /**
     * 导出原料列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:export')")
    @Log(title = "原料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesMaterial mesMaterial)
    {
        List<MesMaterial> list = mesMaterialService.selectMesMaterialList(mesMaterial);
        ExcelUtil<MesMaterial> util = new ExcelUtil<MesMaterial>(MesMaterial.class);
        util.exportExcel(response, list, "原料数据");
    }

    /**
     * 获取原料详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {

        return AjaxResult.success(mesMaterialService.selectMesMaterialById(id));
    }

    /**
     * 新增原料
     */
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "原料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesMaterial mesMaterial)
    {
        if (!mesMaterialService.isMatIdUnique(mesMaterial,true))
        {
            return AjaxResult.error("原料编码重复");
        }
        return toAjax(mesMaterialService.insertMesMaterial(mesMaterial));
    }

    /**
     * 修改原料
     */
    @PreAuthorize("@ss.hasPermi('system:material:edit')")
    @Log(title = "原料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesMaterial mesMaterial)
    {
        if (!mesMaterialService.isMatIdUnique(mesMaterial,false))
        {
            return AjaxResult.error("原料编码重复");
        }
        return toAjax(mesMaterialService.updateMesMaterial(mesMaterial));
    }

    /**
     * 删除原料
     */
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "原料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mesMaterialService.deleteMesMaterialByIds(ids));
    }
}
